package com.zhht.checkcop.parser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zhht.checkcop.db.StaticDBMap;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class ParserMovie implements Pipeline{
	
	private String novelUrl; 
	
	private String type;
	public ParserMovie(){}
	public ParserMovie(String type,String novelUrl){
		this.type = type;
		this.novelUrl = novelUrl;
	}
	public void process(ResultItems resultItems, Task arg1) {
		Map<String, Object> mapResults = resultItems.getAll();
        Iterator<Entry<String, Object>> iter = mapResults.entrySet().iterator();
        Map.Entry<String, Object> entry;
        
        
        List<String> frontCoverUrl=new ArrayList<String>();
        List<String> infoUrl=new ArrayList<String>();
        List<String> movieName=new ArrayList<String>();
        List<String> updateTime=new ArrayList<String>();
        String classifyId="";
        String infoClassifyName="";
        
        // 输出到控制台
        while (iter.hasNext()) {
            entry = iter.next();
            if(entry.getKey().equals("frontCoverUrl")){
            	frontCoverUrl.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("infoUrl")){
            	infoUrl.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("movieName")){
            	movieName.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("updateTime")){
            	updateTime.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("classifyId")){
            	classifyId = entry.getValue() + "";
            }
            if(entry.getKey().equals("infoClassifyName")){
            	infoClassifyName = entry.getValue() + "";
            }
        }
        
        if(null!=infoUrl&&infoUrl.size()>0){
        	SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        	for(int i=0;i<infoUrl.size();i++){
        		Map<String,Object> paramMap=new HashMap<String, Object>();
        		paramMap.put("classifyId", classifyId);
        		paramMap.put("infoClassifyName", infoClassifyName);
        		paramMap.put("frontCoverUrl", frontCoverUrl.get(i));
        		paramMap.put("infoUrl", novelUrl+infoUrl.get(i));
        		paramMap.put("movieName", movieName.get(i));
        		paramMap.put("updateTime", updateTime.get(i));
        		paramMap.put("createTime", sdf.format(new Date()));
        		Map<String, Object> exitMap = StaticDBMap.existNovelNameMap.get(novelUrl+infoUrl.get(i));
        		
        		if(null == exitMap){
        			StaticDBMap.writeNovelNameMap.put(movieName.get(i)+","+infoUrl.get(i), paramMap);
        		}else{
//        			StaticDBMap.updateExistNovelNameMap.put(exitMap.get("id")+"", paramMap);
        		}
        	}
        }
	}

}
